<?php

require_once 'Ext/User/Identify/Abstract.php';

class Ext_User_Identify_Adapter_Ucenter extends Ext_User_Identify_Abstract
{
    const IDENTITY_NOT_FOUND = 'identityNotFound';
    const CREDENTIAL_INVALID = 'credentialInvalid';
    const UNCATEGORIZED      = 'uncategorized';
    protected $_messageTemplates = array(
        self::IDENTITY_NOT_FOUND => "A record with the supplied identity could not be found.",
        self::CREDENTIAL_INVALID => "Supplied credential is invalid.",
        self::UNCATEGORIZED      => "Uncategorized.",
    );
    
    public function verify()
    {
        $result = array(
            'code'     => Ext_User_Identify_Result::FAILURE,
            'data'     => array(),
            'messages' => array(),
        );
        
        $username = $email = '';
        $em = new Zend_Validate_EmailAddress();
        if($em->isValid($this->_data->account)){
            $email = $this->_data->account;
        }else{
            $username = $this->_data->account;
        }
        
        $ucenter = new Ext_Ucenter();
        if($email){
            $ucenterLoginResult = $ucenter->loginByEmail($email, $this->_data->password);
            list($uid, $username, $password, $email) = $ucenterLoginResult;
        }else{
            $ucenterLoginResult = $ucenter->loginByUsername($username, $this->_data->password);
            list($uid, $username, $password, $email) = $ucenterLoginResult;         
        }
        
        if($uid < 1){
            if($uid == -1) {
                $result['messages'][] = $this->_messager->createMessage(self::IDENTITY_NOT_FOUND);

            } elseif($uid == -2) {
                $result['messages'][] = $this->_messager->createMessage(self::CREDENTIAL_INVALID);
                
            } else {
                $result['messages'][] = $this->_messager->createMessage(self::UNCATEGORIZED);
            }
            
            return new Ext_User_Identify_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $userAccountBaseUsernameRow = App_Model_DbTable_User_Account_Base_Username::getInstance()->getRowByUsername($username);
        if(isset($userAccountBaseUsernameRow['userId']) && $userAccountBaseUsernameRow['userId'] > 0){
            $userId = $userAccountBaseUsernameRow['userId'];
            
        }else{
        
            $settings = Zend_Registry::get('settings');
            $row = array(
                'nickname'  => $username,
                'roleId'    => $settings['userDefaultRoleId'],
            );
            $userId = App_Model_DbTable_User::getInstance()->addNew($row);

            if(!$userAccountBaseUsernameRow)
            {
                $row = array(
                    'username'  => $username,
                    'userId'    => $userId,
                );
                App_Model_DbTable_User_Account_Base_Username::getInstance()->addNew($row);
            }
            else 
            {
                $row = array(
                    'userId'    => $userId,
                );
                App_Model_DbTable_User_Account_Base_Username::getInstance()->addOld($row, $username);
            }
        }
        
        $userProfileRow = App_Model_DbTable_User_Profile::getInstance()->getRowByUserId($userId);
        if(!$userProfileRow){
            $row = array (
                'userId'         => $userId,
                'username'       => $username,
            );
            App_Model_DbTable_User_Profile::getInstance()->addNew($row);
            
        }else{
            $row = array();
            if(!$userProfileRow['username']){
                $row['username'] = $username;
            }
            
            if($row){
                App_Model_DbTable_User_Profile::getInstance()->addOld($row, $userId);
            }
        }

        $result['code'] = Ext_User_Identify_Result::SUCCESS;
        $result['data'] = $userId;
        return new Ext_User_Identify_Result($result['code'], $result['data'], $result['messages']);
        
    }
}
